$(document).ready(function() {
    // 获取当前语言
	const language = $("#infoTop").text().length == 4 ? "zh" : "en";
	const itipsText = language == "zh" ? "數據正在上傳中..." : "Data uploading......";
	const tipsText1 = language == "zh" ? "確定要提交該數據嗎？" : "Are you sure you want to submit this data?";
	const tipsText2 = language == "zh" ? "數據提交成功！" : "Operating successfully!";
	const tipsText3 = language == "zh" ? "確定要刪除該數據嗎？" : "Are you sure you want to delete this data?";
	const tipsText4 = language == "zh" ? "數據刪除成功！" : "Operating successfully!";
    const btnText1 = language === "zh" ? "编辑" : "Edit";
    const btnText2 = language === "zh" ? "提交" : "Sub";
    const btnText3 = language === "zh" ? "删除" : "Del";
    /** *************************************************************** */
    let width = $("#infoMenu span").width();
    $("#infoMenu div:eq(0)").css("width", 530 - width);
    $("#form2").attr("target", "_blank");
    /** *************************************************************** */
    if ($("#menuText").val().trim() == "") {
        $("#menuBtn1").attr("disabled", true);
    }
    $("#menuText").keydown(function() {
        if (event.keyCode == 13)
            $("#menuBtn2").click();
    });
    $("#menuBtn1").click(function() {
        window.location.href = "showlist";
    });
    $("#menuBtn2").click(function() {
        var name = $("#menuText").val();
        if (name.trim() != "")
            window.location.href = "showlist?name=" + name;
    });
    /** *************************************************************** */
    /** 新建项目 */
    $("#append").click(function() {
        window.open("insertview");
    });
    /** 导入按钮 */
    $("#import, #ipopup").mouseenter(function() {
        $("#ipopup").show();
    });
    $("#import, #ipopup").mouseleave(function() {
        $("#ipopup").hide();
    });
    $(".con div:eq(0)").click(function() {
    	$("#file1").click();
    });
    $(".con div:eq(1)").click(function() {
    	$("#file2").click();
    });
    $(".con div:eq(2)").click(function() {
        $("#file3").click();
    });

    $("#file1").change(function() {
        if (this.files.length == 0)
            return false;
        $("#tips").text(itipsText);
        $("#form1").submit();
        $("#page").show();
        $("#tips").show();
    });
    $("#file2").change(function() {
        if (this.files.length == 0)
            return false;
        $("#tips").text(itipsText);
        $("#form2").submit();
    });

    $("#file3").attr("webkitdirectory", true);
    $("#file3").change(function () {
        if (this.files.length == 0)
            return false;
        $("#form3").submit();
    });
    /** *************************************************************** */
    var name = $("input[name=name]").val();
    var sort = $("input[name=sort]").val();
    $("#tab1 thead th:eq(1)").click(function() {
    	if (name == null || name == "")
    		location.href = "showlist?sort=name1";
    	else
    		location.href = "showlist?name=" + name + "&sort=name1";
    });
    $("#tab1 thead th:eq(2)").click(function() {
    	if (name == null || name == "")
    		location.href = "showlist?sort=client1";
    	else
    		location.href = "showlist?name=" + name + "&sort=client1";
    });
    $("#tab1 thead th:eq(3)").click(function() {
    	if (name == null || name == "")
    		location.href = "showlist?sort=slope1";
    	else
    		location.href = "showlist?name=" + name + "&sort=slope1";
    });
    $("#tab1 thead th:eq(4)").click(function() {
    	if (name == null || name == "")
    		location.href = "showlist?sort=standard1";
    	else
    		location.href = "showlist?name=" + name + "&sort=standard1";
    });
    $("#tab1 thead th:eq(5)").click(function() {
    	if (name == null || name == "")
    		location.href = "showlist?sort=operator1";
    	else
    		location.href = "showlist?name=" + name + "&sort=operator1";
    });
    $("#tab1 thead th:eq(6)").click(function() {
    	if (name == null || name == "")
    		location.href = "showlist?sort=date1";
    	else
    		location.href = "showlist?name=" + name + "&sort=date1";
    });
    if (sort === "name") {
    	$("#tab1 thead th:eq(1)").text($("#tab1 thead th:eq(1)").text() + "↑");
    } else if (sort === "name1") {
    	$("#tab1 thead th:eq(1)").text($("#tab1 thead th:eq(1)").text() + "↓");
    	$("#tab1 thead th:eq(1)").unbind("click");
    	$("#tab1 thead th:eq(1)").click(function() {
    		if (name == null || name == "")
        		location.href = "showlist?sort=name";
        	else
        		location.href = "showlist?name=" + name + "&sort=name";
        });
    } else if (sort === "client") {
    	$("#tab1 thead th:eq(2)").text($("#tab1 thead th:eq(2)").text() + "↑");
    } else if (sort === "client1") {
    	$("#tab1 thead th:eq(2)").text($("#tab1 thead th:eq(2)").text() + "↓");
    	$("#tab1 thead th:eq(2)").unbind("click");
    	$("#tab1 thead th:eq(2)").click(function() {
        	if (name == null || name == "")
        		location.href = "showlist?sort=client";
        	else
        		location.href = "showlist?name=" + name + "&sort=client";
        });
    } else if (sort === "slope") {
    	$("#tab1 thead th:eq(3)").text($("#tab1 thead th:eq(3)").text() + "↑");
    } else if (sort === "slope1") {
    	$("#tab1 thead th:eq(3)").text($("#tab1 thead th:eq(3)").text() + "↓");
    	$("#tab1 thead th:eq(3)").unbind("click");
    	$("#tab1 thead th:eq(3)").click(function() {
        	if (name == null || name == "")
        		location.href = "showlist?sort=slope";
        	else
        		location.href = "showlist?name=" + name + "&sort=slope";
        });
    } else if (sort === "standard") {
    	$("#tab1 thead th:eq(4)").text($("#tab1 thead th:eq(4)").text() + "↑");
    } else if (sort === "standard1") {
    	$("#tab1 thead th:eq(4)").text($("#tab1 thead th:eq(4)").text() + "↓");
    	$("#tab1 thead th:eq(4)").unbind("click");
    	$("#tab1 thead th:eq(4)").click(function() {
        	if (name == null || name == "")
        		location.href = "showlist?sort=standard";
        	else
        		location.href = "showlist?name=" + name + "&sort=standard";
        });
    } else if (sort === "operator") {
    	$("#tab1 thead th:eq(5)").text($("#tab1 thead th:eq(5)").text() + "↑");
    } else if (sort === "operator1") {
    	$("#tab1 thead th:eq(5)").text($("#tab1 thead th:eq(5)").text() + "↓");
    	$("#tab1 thead th:eq(5)").unbind("click");
    	$("#tab1 thead th:eq(5)").click(function() {
        	if (name == null || name == "")
        		location.href = "showlist?sort=operator";
        	else
        		location.href = "showlist?name=" + name + "&sort=operator";
        });
    } else if (sort === "date") {
    	$("#tab1 thead th:eq(6)").text($("#tab1 thead th:eq(6)").text() + "↑");
    } else if (sort === "date1") {
    	$("#tab1 thead th:eq(6)").text($("#tab1 thead th:eq(6)").text() + "↓");
    	$("#tab1 thead th:eq(6)").unbind("click");
    	$("#tab1 thead th:eq(6)").click(function() {
        	if (name == null || name == "")
        		location.href = "showlist?sort=date";
        	else
        		location.href = "showlist?name=" + name + "&sort=date";
        });
    }
    /** *************************************************************** */
    /** 初始化表格 */
    var name = $("#menuText").val();
    $("#tab1 tbody tr").each(function(n) {
    	var id = $(this).attr("id");
        $(this).find("td:eq(1) a").attr("target", "_blank");
        /** *********************************************************** */
        if (name.trim() != "") {
        	var text = $(this).find("td:eq(1) a").text();
        	var font = "<font color='#f00'>" + name + "</font>";
            var expr = new RegExp(name,"gm");
            var cont = text.replace(expr, font);
            $(this).find("td:eq(1) a").html(cont);
        }
        /** *********************************************************** */
        $(this).find("img").click(function () {
            const text = $(this).prev().text();
            if ($(this).attr("src") === "/cctv/img/展开.png") {
                if ($(this).data("name") === undefined) {
                    $(this).parents("tr").after(getContext(text, id, n));
                    $(this).data("name", "已完成");
                } else
                    $("#tab1 tbody ." + n + "").show();
                $(this).attr("src", "/cctv/img/收起.png");
            } else {
                $("#tab1 tbody ." + n + "").hide();
                $(this).attr("src", "/cctv/img/展开.png");
            }
        });
        /** *********************************************************** */
        $(this).click(function() {
            $("#tab1 tbody tr:even").find("td:eq(0)").css("background-color", "#FAFAFA");
            $("#tab1 tbody tr:odd").find("td:eq(0)").css("background-color", "#EEEEEE");
            $(this).find("td:eq(0)").css("background-color", "#FFD58D");
        });
    });
    /** 编辑项目 */
    $("#tab1 tbody").on("click", "tr td input:nth-child(1)", function () {
        const id = $(this).parents("tr").attr("id");
        window.open("updateview?id=" + id);
    });
    /** 提交项目 */
    $("#tab1 tbody").on("click", "tr td input:nth-child(2)", function () {
        const id = $(this).parents("tr").attr("id");
        if (!confirm(tipsText1))
        	return false;
        $(this).css("background-color", "#ccc");
        $(this).attr("disabled", true);
        if (Ajax("submit", {id: id}))
            showTips(tipsText2);
        setTimeout("location.reload()", 2000);
    });
    /** 删除项目 */
    $("#tab1 tbody").on("click", "tr td input:nth-child(3)", function () {
        const id = $(this).parents("tr").attr("id");
        if (!confirm(tipsText3))
        	return false;
        $(this).css("background-color", "#ccc");
        $(this).attr("disabled", true);
        if (Ajax("delete", {id: id}))
            showTips(tipsText4);
        setTimeout("location.reload()", 2000);
    });
    /** *************************************************************** */
    
    /** *************************************************************** */
    /** 上一页 */
    $(".pagebtn:eq(0)").click(function() {
        var page = Number($("#page1").text()) - 1;
        if (sort == null || sort == "")
        	window.location.href = "showlist?name=" + name + "&page=" + page;
        else
        	window.location.href = "showlist?name=" + name + "&sort=" + sort + "&page=" + page;
    });
    /** 下一页 */
    $(".pagebtn:eq(1)").click(function() {
        var page = Number($("#page1").text()) + 1;
        if (sort == null || sort == "")
        	window.location.href = "showlist?name=" + name + "&page=" + page;
        else
        	window.location.href = "showlist?name=" + name + "&sort=" + sort + "&page=" + page;
    });
    var page1 = $("#page1").text();
    var page2 = $("#page2").text();
    if (page1 <= 1) {
        $(".pagebtn:eq(0)").attr("disabled", true);
        $(".pagebtn:eq(0)").css("color", "#999");
    }
    if (page1 == page2) {
        $(".pagebtn:eq(1)").attr("disabled", true);
        $(".pagebtn:eq(1)").css("color", "#999");
    }
    /** *************************************************************** */
    function getContext(name, id, no) {
        const data = Ajax("viewlist1", {name: name, id: id});
        let context = "";
        for (let i = 0; data != null && i < data.length; i++) {
            context += "<tr id='" + data[i].id + "' class='" + no + "'>";
            context += "  <td>-</td>";
            context += "  <td><a href='editinfo?id=" + data[i].id + "' title='" + data[i].name + "' target='_blank'>" + data[i].name + "</a></td>";
            context += "  <td>" + data[i].client + "</td>";
            context += "  <td>" + data[i].slope + "</td>";
            context += "  <td>" + data[i].standard + "</td>";
            context += "  <td>" + data[i].operator + "</td>";
            context += "  <td>" + data[i].date + "</td>";
            context += "  <td>";
            context += "    <input type='button' style='background-color:#3CB44B;' value='" + btnText1 + "'/>";
            context += "    <input type='button' style='background-color:#2CACF1;' value='" + btnText2 + "'/>";
            context += "    <input type='button' style='background-color:#F34D4D;' value='" + btnText3 + "'/>";
            context += "  </td>";
            context += "</tr>";
        }
        return context;
    }
    
    function showTips(text) {
        $("#tips").show().delay(1800).hide(200);
        $("#tips").text(text);
    }
    function Ajax(url, data) {
        var result = null;
        $.ajax({
            url: url,
            data: data,
            type: "post",
            async: false,
            datatype: "json",
            success: function(data) {
                result = data;
            }
        });
        return result;
    }
});
